Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
function not working on "submit" event, but works when ran in dev console

I am rendering the form below, and below that I have a script. When I load the page, type in the email and password, then submit, it appears to just reload the page (regardless of credentials).

When I type in the email and password, and then execute the function in the dev console by typing loginUser() it executes as expected and if creds are correct it redirects me as it should. If credentials are wrong the alert events fire.

I have put in breakpoints, and on "submit" it gets the email and password that are in the form, but then seems to jump back up to the start of the function (regardless of credentials).

let form = document.getElementById('loginform');
form.addEventListener('submit', loginUser);

function loginUser() {
  let email = document.getElementById('email').value;
  let password = document.getElementById('password').value;
  firebase.auth().signInWithEmailAndPassword(email, password).then(() => {
    window.location.href = "/";
  }).catch(error => {
    console.log(error)
    let errorCode = error.code;
    let errorMessage = error.message;
    if (errorCode == 'auth/wrong-password') {
      window.alert('Entered password was incorrect.');
    } else if (errorCode == 'auth/user-not-found') {
      window.alert('Entered email is invalid.');
    } else {
      window.alert('Please check your login credentials.');
    }
  });
};
firebase.auth().onAuthStateChanged(user => {
  if (user) {
    console.log(user)
    user.getIdToken().then(function(token) {
      //save the token in a cookie  
      document.cookie = "token=" + token;
    });
  } else {
    console.log("What up dude?");
  }
});
<form id="loginform" action="" method="post">
  <div class="form-group">
    <label for="email">Email</label> <input class="form-control" id="email" name="email" required="required" type="text" value="">
  </div>
  <div class="form-group">
    <label for="password">Password</label> <input class="form-control" id="password" name="password" required="required" type="password" value="">
  </div>
  <div class="form-group">
    <input class="btn btn-primary" id="submit" name="submit" type="submit" value="Login">
  </div>
</form>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is imperative to REMOVE or RENAME id="submit" name="submit"

Calling anything submit in a form hides the submit method

Then add preventDefault() to stop submission:

function loginUser(e) {
  e.preventDefault()
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda